package com.example.allshengmedical.util;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Will Joe
 * @url http://blog.csdn.net/wingichoy
 *      <p/>
 *      这是一个简约的折线图 适合展示一个趋势 而并非精确数据
 *      <p/>
 *      Created by Administrator on 2015/12/30.
 */
public class SimpleLineChart extends View {
	// View 的宽和高
	private int mWidth, mHeight;

	// Y轴字体的大小
	private float mYAxisFontSize = 12;

	// 线的颜色
	private int mLineColor = Color.parseColor("#ff0000");

	// 线条的宽度
	private float mStrokeWidth = 2.0f;

	// 点的集合
	private HashMap<Integer, Integer> mPointMap;

	// 点的半径
	private float mPointRadius = 2;

	// 没有数据的时候的内容
	private String mNoDataMsg = "no data";

	// X轴的文字
	private String[] mXAxis = {};

	// Y轴的文字
	private String[] mYAxis = {};

	// 下面目标重复的线的数量
	HashMap<Integer, Integer> lineNums = new HashMap<Integer, Integer>();

	List<HashMap<String, Object>> pointMaps;

	public SimpleLineChart(Context context) {
		this(context, null);
	}

	public SimpleLineChart(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public SimpleLineChart(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);

		if (widthMode == MeasureSpec.EXACTLY) {
			mWidth = widthSize;
		} else if (widthMode == MeasureSpec.AT_MOST) {
			throw new IllegalArgumentException("width must be EXACTLY,you should set like android:width=\"200dp\"");
		}

		if (heightMode == MeasureSpec.EXACTLY) {
			mHeight = heightSize + 100;
		} else if (widthMeasureSpec == MeasureSpec.AT_MOST) {

			throw new IllegalArgumentException("height must be EXACTLY,you should set like android:height=\"200dp\"");
		}

		setMeasuredDimension(mWidth, mHeight + 60);
	}

	@Override
	protected void onDraw(Canvas canvas) {

		if (mXAxis.length == 0 || mYAxis.length == 0) {
			throw new IllegalArgumentException("X or Y items is null");
		}
		// 画坐标线的轴

		Paint axisPaint = new Paint();
		Paint textPaint = new Paint();
		textPaint.setColor(Color.parseColor("#3c3c3c"));
		textPaint.setTextSize(13);

		axisPaint.setAntiAlias(true);
		canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));// 设置线条等图形的抗锯齿
		axisPaint.setTextSize(mYAxisFontSize);
		axisPaint.setColor(Color.parseColor("#c0c0c0"));

		if (mPointMap == null || mPointMap.size() == 0) {
			int textLength = (int) axisPaint.measureText(mNoDataMsg);
			canvas.drawText(mNoDataMsg, mWidth / 2 - textLength / 2, mHeight / 2, axisPaint);
		} else {
			// 画 Y 轴

			// 存放每个Y轴的坐标
			int[] yPoints = new int[mYAxis.length];

			// 计算Y轴 每个刻度的间距
			int yInterval = (int) ((mHeight - mYAxisFontSize - 2) / (mYAxis.length));

			// 测量Y轴文字的高度 用来画第一个数
			Paint.FontMetrics fm = axisPaint.getFontMetrics();
			int yItemHeight = (int) Math.ceil(fm.descent - fm.ascent);

			Log.e("wing", mHeight + "");
			for (int i = 0; i < mYAxis.length; i++) {
				canvas.drawText(mYAxis[i], 0, mYAxisFontSize + i * yInterval, axisPaint);

				yPoints[i] = (int) (mYAxisFontSize + i * yInterval);
				if (i % 2 == 0) {
					canvas.drawLine(0, yPoints[i], mWidth, yPoints[i], axisPaint);
				}

			}
			// canvas.drawLine(0, 0, mWidth, 0, axisPaint);
			// canvas.drawLine(0, mHeight, mWidth, mHeight, axisPaint);
			canvas.drawLine(0, 12, 0, mHeight - 28, axisPaint);
			canvas.drawLine(mWidth - 1, 12, mWidth - 1, mHeight - 28, axisPaint);

			// 画 X 轴

			// x轴的刻度集合
			int[] xPoints = new int[mXAxis.length];

			Log.e("wing", xPoints.length + "");
			// 计算Y轴开始的原点坐标
			int xItemX = (int) axisPaint.measureText(mYAxis[1]);

			// X轴偏移量
			int xOffset = 53;
			// 计算x轴 刻度间距
			int xInterval = (int) 64;
			// 获取X轴刻度Y坐标
			int xItemY = (int) (mYAxisFontSize + mYAxis.length * yInterval);

			for (int i = 0; i < mXAxis.length; i++) {
				canvas.drawText(mXAxis[i], i * xInterval + xItemX + xOffset + 18, xItemY-13, axisPaint);
				// xPoints[i] = (int) (i * xInterval + xItemX
				// + axisPaint.measureText(mXAxis[i]) / 2 + xOffset + 10);

				// xPoints[i] = (int) (i * xInterval + xItemX + xOffset + 10);
				xPoints[i] = (int) (i * xInterval + xItemX - axisPaint.measureText(mXAxis[i]) / 2 + xOffset + 10);

				// Log.e("wing", xPoints[i] + "");

				canvas.drawLine(xPoints[i], 12, xPoints[i], mHeight - 28, axisPaint);
				if (i % 2 == 1) {
					Paint axisPaint1 = new Paint();
					axisPaint1.setStrokeWidth(3.0f);
					axisPaint1.setColor(Color.parseColor("#dbdbdb"));
					canvas.drawLine(xPoints[i], 12, xPoints[i], mHeight - 28, axisPaint1);
				}
			}

			// 画点
			Paint pointPaint = new Paint();

			pointPaint.setColor(mLineColor);

			Paint linePaint = new Paint();

			linePaint.setColor(mLineColor);
			linePaint.setAntiAlias(true);
			canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
			// 设置线条宽度
			linePaint.setStrokeWidth(mStrokeWidth);
			pointPaint.setStyle(Paint.Style.FILL);

			for (int i = 1; i <= 20; i++) {
				lineNums.put(i, 0);
			}
			for (int i = 0; i < mXAxis.length; i++) {
				if (mPointMap.get(i) == null) {
					throw new IllegalArgumentException("PointMap has incomplete data!");
				}
				// 不能超过三层循环

				ArrayList<ArrayList<Integer>> mid = new ArrayList<ArrayList<Integer>>();
				for (int j = 0; j < pointMaps.size(); j++) {
					if (TextUtils.isEmpty(pointMaps.get(j).get("gotTo") + "")) {

					} else {
						if ("null".equals(pointMaps.get(j).get("gotTo") + "")) {

						} else {
							ArrayList<Integer> little = new ArrayList<Integer>();
							little.add(Integer.parseInt(pointMaps.get(j).get("gotTo") + ""));
							little.add(j + 1);
							mid.add(little);
						}
					}

				}

				// 画点
				canvas.drawCircle(xPoints[i], yPoints[mPointMap.get(i)], mPointRadius, pointPaint);
				if (i > 0) {
					canvas.drawLine(xPoints[i - 1], yPoints[mPointMap.get(i - 1)], xPoints[i],
							yPoints[mPointMap.get(i)], linePaint);

					if ((i - 1) % 2 == 0) {  
						int count = 0;
						for (int j = 0; j < mid.size(); j++) {
							if(!TextUtils.isEmpty(pointMaps.get(i / 2).get("gotTo") + "")){
								if (mid.get(j).get(0) >= Integer.parseInt(pointMaps.get(i / 2).get("gotTo") + "")) {
									if (mid.get(j).get(1) <i/2+1) {
										count++;
									}
								}
							}
							
						}

						if (!"".equals(pointMaps.get(i / 2).get("gotTo").toString())) {

							if (count == 0) {
								canvas.drawLine(xPoints[i - 1] + 32, mHeight - 28, xPoints[i - 1] + 32,
										mHeight - 28 + 10 + 20, linePaint);

								canvas.drawLine(
										xPoints[Integer.parseInt(pointMaps.get(i / 2).get("gotTo").toString()) * 2 - 1]
												- 32,
										mHeight - 28,
										xPoints[Integer.parseInt(pointMaps.get(i / 2).get("gotTo").toString()) * 2 - 1]
												- 32,
										mHeight - 28 + 10 + 20, linePaint);
								canvas.drawLine(xPoints[i - 1] + 32, mHeight - 28 + 10 + 20,
										xPoints[Integer.parseInt(pointMaps.get(i / 2).get("gotTo").toString()) * 2 - 1]
												- 32,
										mHeight - 28 + 10 + 20, linePaint);

								// canvas.drawText(pointMaps.get(i /
								// 2).get("repititions")
								// .toString()
								// , (xPoints[Integer.parseInt(pointMaps
								// .get(i / 2).get("gotTo")
								// .toString()) * 2 - 1] - 32-xPoints[i - 1] +
								// 32)/2,
								// mHeight - 28 + 10 + 20, textPaint);
								canvas.drawText("* " + pointMaps.get(i / 2).get("repititions").toString(),
										((xPoints[i - 1] + 32) - (xPoints[Integer
												.parseInt(pointMaps.get(i / 2).get("gotTo").toString()) * 2 - 1] - 32))
										/ 2
										+ xPoints[Integer.parseInt(pointMaps.get(i / 2).get("gotTo").toString()) * 2
												- 1] - 32 - 15,
										mHeight - 28 + 10 + 15, textPaint);

							} else if (count == 1) {

								canvas.drawLine(xPoints[i - 1] + 32, mHeight - 28, xPoints[i - 1] + 32,
										mHeight - 28 + 21 + 40, linePaint);

								canvas.drawLine(
										xPoints[Integer.parseInt(pointMaps.get(i / 2).get("gotTo").toString()) * 2 - 1]
												- 32,
										mHeight - 28,
										xPoints[Integer.parseInt(pointMaps.get(i / 2).get("gotTo").toString()) * 2 - 1]
												- 32,
										mHeight - 28 + 21 + 40, linePaint);
								canvas.drawLine(xPoints[i - 1] + 32, mHeight - 28 + 21 + 40,
										xPoints[Integer.parseInt(pointMaps.get(i / 2).get("gotTo").toString()) * 2 - 1]
												- 32,
										mHeight - 28 + 21 + 40, linePaint);

								canvas.drawText("* " + pointMaps.get(i / 2).get("repititions").toString(),
										((xPoints[i - 1] + 32) - (xPoints[Integer
												.parseInt(pointMaps.get(i / 2).get("gotTo").toString()) * 2 - 1] - 32))
										/ 2
										+ xPoints[Integer.parseInt(pointMaps.get(i / 2).get("gotTo").toString()) * 2
												- 1] - 32 - 15,
										mHeight - 28 + 21 + 40 - 10, textPaint);

							} else if (count == 2) {
								canvas.drawLine(xPoints[i - 1] + 32, mHeight - 28, xPoints[i - 1] + 32,
										mHeight - 28 + 27 + 60, linePaint);

								canvas.drawLine(
										xPoints[Integer.parseInt(pointMaps.get(i / 2).get("gotTo").toString()) * 2 - 1]
												- 32,
										mHeight - 28,
										xPoints[Integer.parseInt(pointMaps.get(i / 2).get("gotTo").toString()) * 2 - 1]
												- 32,
										mHeight - 28 + 27 + 60, linePaint);
								canvas.drawLine(xPoints[i - 1] + 32, mHeight - 28 + 27 + 60,
										xPoints[Integer.parseInt(pointMaps.get(i / 2).get("gotTo").toString()) * 2 - 1]
												- 32,
										mHeight - 28 + 27 + 60, linePaint);

								canvas.drawText("* " + pointMaps.get(i / 2).get("repititions").toString(),
										((xPoints[i - 1] + 32) - (xPoints[Integer
												.parseInt(pointMaps.get(i / 2).get("gotTo").toString()) * 2 - 1] - 32))
										/ 2
										+ xPoints[Integer.parseInt(pointMaps.get(i / 2).get("gotTo").toString()) * 2
												- 1] - 32 - 15,
										mHeight - 28 + 27 + 60 - 8, textPaint);
							} else {
								ByAlert.alert("1");
							}

							lineNums.put(Integer.parseInt(pointMaps.get(i / 2).get("gotTo").toString()),
									lineNums.get(Integer.parseInt(pointMaps.get(i / 2).get("gotTo").toString())) + 1);
						}

						canvas.drawText(pointMaps.get(i / 2).get("time").toString(), xPoints[i - 1] + 12,
								yPoints[mPointMap.get(i - 1)] - 3, textPaint);

						canvas.drawText(pointMaps.get(i / 2).get("temperature").toString() + "℃", xPoints[i - 1] + 30,
								yPoints[mPointMap.get(i - 1)] + 16, textPaint);
						if (!"".equals(pointMaps.get(i / 2).get("deltaT").toString())) {
							canvas.drawText(

									"+" + pointMaps.get(i / 2).get("deltaT").toString() + "℃", xPoints[i - 1] - 45,
									yPoints[mPointMap.get(i - 1)] - 3, textPaint);
						}

					}

				}

			}

		}

	}

	/**
	 * 设置map数据
	 * 
	 * @param data
	 */
	public void setData(HashMap<Integer, Integer> data) {
		mPointMap = data;
		invalidate();
	}

	/**
	 * 设置map数据
	 * 
	 * @param data
	 */
	public void setData2(List<HashMap<String, Object>> pointMaps) {
		this.pointMaps = pointMaps;
		invalidate();
	}

	/**
	 * 设置Y轴文字
	 * 
	 * @param yItem
	 */
	public void setYItem(String[] yItem) {
		mYAxis = yItem;
	}

	/**
	 * 设置X轴文字
	 * 
	 * @param xItem
	 */
	public void setXItem(String[] xItem) {
		mXAxis = xItem;
	}

	public void setLineColor(int color) {
		mLineColor = color;
		invalidate();
	}

	// /**
	// * 画所有横向表格，包括X轴
	// */
	// private void drawAllXLine(Canvas canvas) {
	// for (int i = 0; i < spacingHeight + 1; i++) {
	// canvas.drawLine(blwidh, bheight - (bheight / spacingHeight) * i
	// + marginTop, (canvasWidth - blwidh), bheight
	// - (bheight / spacingHeight) * i + marginTop, mPaint);
	//
	// }
	// }
	//
	// /**
	// * 画所有纵向表格，包括Y轴
	// */
	// private void drawAllYLine(Canvas canvas) {
	// for (int i = 0; i < mYAxis.length + 1; i++) {
	//
	// canvas.drawLine(blwidh + (canvasWidth - blwidh) / yRawData.size()
	// * i, marginTop,
	// blwidh + (canvasWidth - blwidh) / yRawData.size() * i,
	// bheight + marginTop, mPaint);
	//
	// }
	// }
}